#include "Socket.hpp"
#include <poll.h>

const static int fd_num_max = 64; // fd最大数量
const int defaultport = 8888;
const int non_event = 0; //_event_fds数组默认初始化
const int defaultfd = -1;

class Pollserver
{
public:
    Pollserver(const int port = defaultport) : _port(port)
    {
        for (int i = 0; i < fd_num_max; i++)
        {
            _event_fds[i].fd = defaultfd;
            _event_fds[i].events = non_event;
            _event_fds[i].revents = non_event;
        }
    }

    ~Pollserver()
    {
        _listensock.Close();
    }

public:
    void Init()
    {
        _listensock.Socket();
        _listensock.Bind(_port);
        _listensock.Listen();
    }

    void Accepter()
    {
        std::string clientip;
        uint16_t clientport;
        int newfd = _listensock.Accept(&clientip, &clientport);
        if (newfd < 0)
            return;

        int pos;
        for (pos = 0; pos < fd_num_max; pos++)
        {
            if (_event_fds[pos].fd != defaultfd)
                continue;

            else
                break;
        }

        if (pos >= fd_num_max)
        {
            lg(Warning, "server is full, close fd: %d", newfd);
            close(newfd);
        }
        else
        {
            _event_fds[pos].fd = newfd;
            _event_fds[pos].events = POLLIN;
            _event_fds[pos].revents = non_event;    
        }
    }

    void Recver(int fd, int pos)
    {
        char buffer[1024];
        ssize_t n = read(fd, buffer, sizeof(buffer)-1); //会有粘包问题 我们暂时不考虑 
        if (n > 0)
        {
            buffer[n - 1] = '\0';
            std::cout << "get a message: " << buffer << std::endl;
        }
        else if (n == 0)
        {
            lg(Info, "client quit, me too, close fd: %d", fd);
            close(fd);
            _event_fds[pos].fd = defaultfd;
            
        }
        else
        {
            lg(Warning, "recv error, close fd:", fd);
            close(fd);
            _event_fds[pos].fd = defaultfd;
            
        }
    }

    void Dispatcher()
    {
        for (int i = 0; i < fd_num_max; i++)
        {
            if (_event_fds[i].fd == defaultfd)
                continue;

            if (_event_fds[i].revents != non_event)
            {
                if (_event_fds[i].fd == _listensock.Getfd())
                {
                    Accepter();
                }
                else
                {
                    Recver(_event_fds[i].fd, i);
                }
            }
        }
    }

    void Start()
    {
        _event_fds[0].fd = _listensock.Getfd();
        _event_fds[0].events = POLLIN;
        int timeout = 3000; // 单位毫秒

        for (;;)
        {
            int n = poll(_event_fds, fd_num_max, timeout);
            switch (n)
            {
            case -1:
                std::cout << "poll error" << std::endl;
                break;
            case 0:
                std::cout << "time out" << std::endl;
                break;
            default:
                //std::cout << "get a new link!!!" << std::endl;
                Dispatcher();
                break;
            }
        }
    }

private:
    Sock _listensock;
    uint16_t _port;

    struct pollfd _event_fds[fd_num_max]; // 用户自己维护的数组
};